package dlut.alanhome.trafficview;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;


/*
 * todo:
 * 1.find some avalable IP about traffic and show in google maps of BeiJing
 * 2.design the video activity UI
 */
public class trafficview extends MapActivity {
    /** Called when the activity is first created. */
	 MapView mapView;
	 List<Overlay> mapOverlays;
	 MapController mapController; 
	 GeoPoint mGeoPoint;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(dlut.alanhome.trafficview.R.layout.main);
       
        Toast.makeText(this, "locating your location...", Toast.LENGTH_LONG).show(); 
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setTraffic(true);  
        mapController = mapView.getController(); 
		mapView.setEnabled(true);
		mapView.setClickable(true);
		//设置地图支持缩放
		mapView.setBuiltInZoomControls(true); 
	
// 北京的位置
	
		mGeoPoint = new GeoPoint((int) (39.9 * 1E6), (int) (116.39 * 1E6));
//定位到北京
		mapController.animateTo(mGeoPoint);		
		
		//设置倍数(1-21)
		
		
		mapController.setZoom(13);

        
        mapOverlays = mapView.getOverlays();
        Drawable location = this.getResources().getDrawable(dlut.alanhome.trafficview.R.drawable.location);
        Drawable drawable = this.getResources().getDrawable(dlut.alanhome.trafficview.R.drawable.androidmarker);
        drawable.setBounds(0, 0, 30,30);
        location.setBounds(0,0, 30,30);
        HelloItemizedOverlay locationoverlay = new HelloItemizedOverlay(location,trafficview.this);
        GeoPoint pointz = new GeoPoint((int) (39.895 * 1E6), (int) (116.39 * 1E6));
        OverlayItem overlayitemz = new OverlayItem(pointz, "beijing", "beijing");
        locationoverlay.addOverlay( overlayitemz);
        mapOverlays.add(locationoverlay);
        
        HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,trafficview.this);
        GeoPoint point = new GeoPoint((int) (39.905 * 1E6), (int) (116.395 * 1E6));
        OverlayItem overlayitem = new OverlayItem(point, "beijing", "beijing");
        GeoPoint point2 = new GeoPoint((int) (39.91 * 1E6), (int) (116.38 * 1E6));
        OverlayItem overlayitem2 = new OverlayItem(point2, "beijing", "beijing");
        GeoPoint point3= new GeoPoint((int) (39.8902 * 1E6), (int) (116.37 * 1E6));
        OverlayItem overlayitem3 = new OverlayItem(point3, "beijing", "212.211.133.190/axis-cgi/jpg/image.cgi");
        GeoPoint point4= new GeoPoint((int) (39.87 * 1E6), (int) (116.36 * 1E6));
        OverlayItem overlayitem4 = new OverlayItem(point4, "beijing", "www.3dcam.co.uk/axis-cgi/jpg/image.cgi?resolution=640x480");
        GeoPoint point5= new GeoPoint((int) (39.8902 * 1E6), (int) (116.35 * 1E6));
        OverlayItem overlayitem5 = new OverlayItem(point5, "beijing", "216.8.159.21/axis-cgi/jpg/image.cgi");
        GeoPoint point6= new GeoPoint((int) (39.92 * 1E6), (int) (116.34 * 1E6));
        OverlayItem overlayitem6 = new OverlayItem(point6, "beijing", "217.169.130.236/axis-cgi/jpg/image.cgi");
        GeoPoint point7= new GeoPoint((int) (39.8903 * 1E6), (int) (116.33 * 1E6));
        OverlayItem overlayitem7 = new OverlayItem(point7, "beijing", "194.177.131.229/axis-cgi/jpg/image.cgi");
        GeoPoint point8= new GeoPoint((int) (39.93 * 1E6), (int) (116.32 * 1E6));
        OverlayItem overlayitem8 = new OverlayItem(point8, "beijing", "87.139.20.112/axis-cgi/jpg/image.cgi");
        GeoPoint point9= new GeoPoint((int) (39.94 * 1E6), (int) (116.31 * 1E6));
        OverlayItem overlayitem9 = new OverlayItem(point9, "beijing", "87.101.127.24/axis-cgi/jpg/image.cgi");
        GeoPoint point10= new GeoPoint((int) (39.9 * 1E6), (int) (116.30 * 1E6));
        OverlayItem overlayitem10 = new OverlayItem(point10, "beijing", "orovalleycam.axiscam.net:7697/axis-cgi/jpg/image.cgi");
        GeoPoint point11= new GeoPoint((int) (39.9 * 1E6), (int) (116.31 * 1E6));
        OverlayItem overlayitem11 = new OverlayItem(point11, "beijing", "82.95.192.129:443/axis-cgi/jpg/image.cgi");
        
        itemizedoverlay.addOverlay(overlayitem);
        itemizedoverlay.addOverlay(overlayitem2);
        itemizedoverlay.addOverlay(overlayitem3);
        itemizedoverlay.addOverlay(overlayitem4);
        itemizedoverlay.addOverlay(overlayitem5);
        itemizedoverlay.addOverlay(overlayitem6);
        itemizedoverlay.addOverlay(overlayitem7);
        itemizedoverlay.addOverlay(overlayitem8);
        itemizedoverlay.addOverlay(overlayitem9);
        itemizedoverlay.addOverlay(overlayitem10);
        itemizedoverlay.addOverlay(overlayitem11);
               
        mapOverlays.add(itemizedoverlay);
        
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}


class HelloItemizedOverlay extends ItemizedOverlay {
	
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	 Context mContext;
	public HelloItemizedOverlay(Drawable defaultMarker) {
		super(defaultMarker);
		// TODO Auto-generated constructor stub
	}
	public void addOverlay(OverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate();
	}
	
	@Override
	protected OverlayItem createItem(int arg0) {
		// TODO Auto-generated method stub
		return mOverlays.get(arg0);
	}
	public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
		  super(defaultMarker);
		  mContext = context;
		}
	@Override
	public int size() {
		// TODO Auto-generated method stub
		  return mOverlays.size();
	}
	
	@Override
	protected boolean onTap(int index) {
		OverlayItem item;
				
	  Intent i = new Intent(mContext, videostream.class);
	  i.putExtra("idx", index);
	  if(index >= 2) {
		  item=mOverlays.get(index);
		  i.putExtra("strurl", item.getSnippet());
	  }
	  mContext.startActivity(i);
	  
	  return true;
	}
}
